<template>
	<view class="container">
		<view class="row-a1">
			<view class="logo">
				<u-icon name="weixin-fill" size="60" color="#5ac725"></u-icon>
			</view>
			<view class="title">
				欢迎使用课程小程序~
			</view>
			<view class="btn">
				<u-button icon="weixin-fill" type="success" shape="circle" open-type="getPhoneNumber"
					@getphonenumber="submit">
					手机号授权登录
				</u-button>
			</view>
			<view class="look" @click="tourist">
				游客登录
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		mapMutations
	} from "vuex";
	import {
		login,
		checkSession
	} from "@/api/login.js"
	export default {
		data() {
			return {

			}
		},
		components: {

		},
		methods: {
			...mapMutations(['setUserInfo']),
			getPhoneNumber(e) {
				let that = this;
				console.log(e);
				uni.login({
					success(res) {
						console.log(res);
						if (e.detail.code) {
							login({
								code: e.detail.code,
								encryptedData: e.detail.encryptedData,
								iv: e.detail.iv,
								js_code: res.code
							}).then(res => {
								console.log(res);
							});
						} else {
							that.$refs.uToast.show({
								type: "error",
								message: "授权失败",
							});
						}
					}
				});
			},
			submit() {
				let that = this;
				wx.login({
					success(res) {
						if (res.code) {
							login({
								js_code: res.code
							}).then(res => {
								if (res.code == 200) {
									uni.setStorageSync("token", res.data.token);
									uni.setStorageSync("userInfo", res.data);
									that.setUserInfo(res.data);
									that.goHome();
								} else {
									that.$refs.uToast.show({
										type: "error",
										message: "登录授权失败",
									});
								}
							});
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			},
			tourist() {
				// let user = {
				// 	"avatar": "https://intstu-4g5o0w3q58960fe0-1258616199.tcloudbaseapp.com/resources/2025-08/lowcode-2353055",
				// 	"desc": "追逐梦想,拥抱变化,相信一切皆有可能!",
				// 	"name": "同学",
				// 	"role": 1,
				// 	"roles": [1],
				// 	"teacherStatus": 1
				// };
				let user = {
					"role": 2,
					"teacherProfile": "前雅思考官，10年雅思教学经验，擅长写作和口语提分，学生平均提分1.5-2分。",
					"updateBy": "1950343464140222465",
					"openid": "o5oU81w4QiTJyN2rhUxRPwkOpY_o",
					"roles": [1, 2, 3],
					"profile": "欣欣向荣",
					"name": "张三",
					"mobile": "13888888888",
					"_id": "f51ab3f068a3e46002758b98679fa282",
					"updatedAt": 1755830516569,
					"token": "ZLKXALpM+4R8Fcg8dHiZYQ=="
				}
				// uni.setStorageSync("token", "12345678901234567890");
				this.setUserInfo(user);
				this.goHome();
			},
			goHome() {
				uni.$u.route({
					url: "/pages/index/index",
					type: "reLaunch"
				})
			}
		},
		created() {

		},
		onShow() {

		},
		onLoad() {},
		onLaunch() {

		}
	}
</script>

<style lang="scss">
	.container {
		background-color: #f5f5f5;
		height: 100vh;
		position: relative;

		.row-a1 {
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
			display: flex;
			flex-direction: column;
			align-items: center;
			width: 100%;

			.logo {
				width: 80px;
				height: 80px;
				background-color: #fff;
				border-radius: 50%;
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;
				margin-bottom: 10px;
				position: relative;
			}

			.title {
				text-align: center;
				margin-bottom: 240px;
				font-weight: bold;
			}

			.btn {
				margin-bottom: 10px;
				width: 240px;
			}

			.look {
				font-size: 28rpx;
				text-align: right;
				width: 240px;
				color: $u-info;
			}
		}
	}
</style>